; (function (window, $) {
  // paging
  var page1 = paging()

  function choose() {
    var statusid = $("input[name='statusid']").val()//点击来哪个状态
    var page = $("input[name='page']").val()

    util.ajax({
      url:
        '/member/apply/list' +
        '?status=' +
        statusid +
        '&page=' +
        page +
        '&limit=6',
      type: 'post',
      success: function (res) {
        page1.init($("input[name='page']").val(), res.data.total)
        console.log(res.data.rows)
        var applylist = res.data.rows
        $('.applylist').html('')
        for (var i in applylist) {
          var tr = $('<tr></tr>').appendTo($('.applylist'))

          //申请产品

          var td1 = $('<td><a href="javascript:;"></a></td>')
            .appendTo(tr)
            .html(applylist[i].instName + '-' + applylist[i].productName)

          //借款金额：

          var loanAm = [
            '1万',
            '2万',
            '3万',
            '4万',
            '5万',
            '10万',
            '15万',
            '20万',
            '30万',
            '50万',
            '100万',
            '200万',
            '300万',
            '500万',
            '1000万'
          ]
          if (applylist[i].loanAmount == undefined) {
            var td2 = $('<td></td>')
              .appendTo(tr)
              .html()
          } else {
            var nub = applylist[i].loanAmount
            var money = loanAm[nub]
            var td2 = $('<td></td>')
              .appendTo(tr)
              .html(money)
          }
          //申请时间

          var td3 = $('<td></td>')
            .appendTo(tr)
            .html(applylist[i].createTime.substring(0, 10))

          //申请渠道

          var td4 = $('<td></td>')
            .appendTo(tr)
            .html(applylist[i].managerName)

          //放款时间
          var td5
          applylist[i].status == 4
            ? (td5 = $('<td></td>')
              .appendTo(tr)
              .html(applylist[i].resultTime.substring(0, 10)))
            : (td5 = $('<td></td>')
              .appendTo(tr)
              .html())

          //订单状态
          //received_status == 0:未领取  1:已领取  2:超时作废  3:已被领取
          var loanstatus = [
            '待审核',
            '已审核',
            '进行中',
            '放款中',
            '成功',
            '失败'
          ]
          if (applylist[i].status == undefined) {
            var td6 = $('<td><a class="applyState"></a></td>').appendTo(tr)
          } else {
            if (applylist[i].received_status > 1) {//如果该客户已被一个a渠道领取，那么b,c,d渠道的状态就是超时无法领取
              var td6 = $(
                '<td><a class="applyState">' + '超时无法领取' + '</a></td>'
              ).appendTo(tr)
            } else if (applylist[i].received_status == 1 && applylist[i].status == 0) {
              var td6 = $(
                '<td><a class="applyState">' + '已审核' + '</a></td>'
              ).appendTo(tr)
            } else {
              var stnum = applylist[i].status
              var status = loanstatus[stnum]
              var td6 = $(
                '<td><a class="applyState">' + status + '</a></td>'
              ).appendTo(tr)
            }
          }

          //操作canceled
          var opera = ['中止申请', '', '删除', '评价', '/删除', '已中止申请']
          //status  用户订单状态 0:待处理 1：已处理 2：进行中 3：放款中 4：成功 5：失败
          //待处理 ：空
          //已处理 进行中：中止申请
          //放款中：空
          //成功：评价  /删除
          //失败：删除
          switch (applylist[i].status) {
            case 0:
              var td7 = $('<td></td>').appendTo(tr)
              break
            case 1:
            case 2:
              if (applylist[i].received_status > 1) {
                var td7 = $('<td></td>').appendTo(tr)
              } else {
                var td7 = $('<td></td>').appendTo(tr)
                var a = $('<a class="opera stopbtn"></a>')
                  .appendTo(td7)
                  .attr('data-id', applylist[i].id)
                  .attr('data-productid', applylist[i].productId)
                  .attr('data-managerid', applylist[i].managerId)
                  .html(opera[0])
              }
              break
            case 3:
              var td7 = $('<td></td>').appendTo(tr)
              break
            case 4:
              if (applylist[i].received_status > 1) {
                var td7 = $('<td></td>').appendTo(tr)
              } else {
                var td7 = $('<td></td>').appendTo(tr)
                if (applylist[i].commented == 0) {
                  var a1 = $('<a class="opera reviewbtn"></a>')
                    .appendTo(td7)
                    .attr('data-id', applylist[i].id)
                    .attr('data-productid', applylist[i].productId)
                    .attr('data-managerid', applylist[i].managerId)
                    .html(opera[3])
                } else {
                  td7.html('已评价').css('color', '#A9A9A9')
                }
              }

              // var a2=$('<a class="opera delbtn"></a>').appendTo(td7).attr("data-id",applylist[i].productId).html(opera[4]);
              break
            case 5:
              var td7 = $('<td></td>').appendTo(tr)
              // var a=$('<a class="opera delbtn"></a>').appendTo(td7).attr("data-id",applylist[i].productId).html(opera[2]);
              break
            default:
              break
          }

          //已中止申请

          if (applylist[i].canceled == 1) {
            if (td6.text() === '待审核') {
              td7.html('')
            } else {
              td7
                .html(opera[5])
                .css('color', '#A9A9A9')
                .attr('disabled', true)
            }
          }
        }
        // for  end

        //中止申请
        $('.stopbtn').on('click', function () {
          var id = $(this).data('id')
          var thisStop = $(this)
          $('.stop').css('display', 'block')
          $('.stopdigbtn').click(function () {
            var btn = $(this).data('btn')
            var stopReasonSelect = $('.stopReasonSelect option:selected').val()
            var stopReasonText = $('.stopReasonText').val()
            var reason = stopReasonSelect + '其他原因：' + stopReasonText
            if (btn == 'yes') {
              util.ajax({
                url: '/member/apply/modifyCancel/' + id + '?reason=' + reason,
                type: 'get',
                success: function (res) {
                  console.log(res)
                }
              })
              $('.stop').css('display', 'none')
              thisStop
                .html('已中止申请')
                .off('click')
                .css('color', '#A9A9A9')
                .attr('class', 'opera')
            } else if (btn == 'no') {
              $('.stop').css('display', 'none')
              return
            }
          })
        })

        //评价
        $('.reviewbtn').on('click', function () {
          var thisreview = $(this)
          var id = $(this).data('id')
          var productId = $(this).data('productid')
          var managerId = $(this).data('managerid')
          $('.rate').css('display', 'block')
          $('.ratebtn').click(function () {
            var btn = $(this).data('btn')
            var reviewChannel = $('#reviewChannel').val()
            var reviewPro = $('#reviewPro').val()
            var channelScore = $('#channelscore').val()
            var productScore = $('#productscore').val()

            if (btn == 'yes') {
              util.ajax({
                url:
                  '/member/comment/manager/' +
                  managerId +
                  '?content=' +
                  reviewChannel +
                  '&score=' +
                  channelScore,
                type: 'post',
                success: function (res) {
                  console.log(res)
                }
              })
              util.ajax({
                url:
                  '/member/comment/product/' +
                  productId +
                  '?content=' +
                  reviewPro +
                  '&score=' +
                  productScore +
                  '&applyId=' +
                  id,
                type: 'post',
                success: function (res) {
                  console.log(res)
                }
              })
              thisreview.css('display', 'none')
              $('.rate').css('display', 'none')
            } else {
              $('.rate').css('display', 'none')
              return
            }
          })
        })
      }
      // success end
    })
  }

  $(function () {
    //paging
    page1.init(1, 0, $('#page1'), function () {
      $("input[name='page']").val($(this).data('page'))
      choose()
    })

    //点击筛选
    $('.choose').on('click', function () {
      $("input[name='statusid']").val($(this).data('id'))
      $(this)
        .attr('class', 'choose active')
        .siblings()
        .removeClass('active')
      $('.applylist').html('')
      choose()
    })

    initRate()
    choose()
  })

  function initRate() {
    $('.rate-list a').hover(
      function () {
        $(this)
          .closest('.rate-box')
          .addClass('r' + $(this).index())
      },
      function () {
        $(this)
          .closest('.rate-box')
          .removeClass('r' + $(this).index())
      }
    )

    $('.rate-list a').on('click', function () {
      $(this)
        .closest('.rate-box')
        .removeClass()
        .addClass('rate-box')
      $(this)
        .closest('.rate-box')
        .addClass('ra' + $(this).index())
      $(this)
        .closest('.rate-box')
        .find("input[type='hidden']")
        .val(parseInt($(this).index() + 1))
    })
  }
})(window, jQuery)
